package cn.broadsense.web.controller.biz;

import cn.broadsense.biz.bo.LayerAreaBo;
import cn.broadsense.biz.bo.LayerBo;
import cn.broadsense.biz.domain.Channel;
import cn.broadsense.biz.domain.Layer;
import cn.broadsense.biz.dto.LayerAreaDto;
import cn.broadsense.biz.service.ChannelService;
import cn.broadsense.biz.service.LayerAreaService;
import cn.broadsense.biz.service.LayerService;
import cn.broadsense.common.annotation.Anonymous;
import cn.broadsense.common.annotation.Log;
import cn.broadsense.common.core.controller.BaseController;
import cn.broadsense.common.core.domain.R;
import cn.broadsense.common.core.page.TableDataInfoResult;
import cn.broadsense.common.enums.BusinessType;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author: pengxin
 * @create: 2024-09-10 15:34
 * @Version 1.0
 **/
@Api(tags = "图层管理")
@RestController
@RequestMapping("/layer")
@RequiredArgsConstructor
public class LayerController extends BaseController {

    private final LayerService layerService;

    private final LayerAreaService layerAreaService;

    private final ChannelService channelService;


    @ApiOperation(value = "获取图层列表", notes = "获取图层列表")

    @GetMapping("/list")
    public R<List<Layer>> list(String name) {
        List<Layer> list = layerService.listAll(name);
        return R.ok(list);
    }

    @Log(title = "图层管理-新增图层", businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增图层", notes = "新增图层")
    @PostMapping("/save")
    public R<Void> save(@RequestBody @Validated LayerBo layerBo) {
        layerService.saveLayer(layerBo);
        return R.ok();
    }

    @Log(title = "图层管理-修改图层", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改图层", notes = "修改图层")
    @PutMapping("/update/{id}")
    public R<Void> update(@PathVariable Long id, @RequestBody @Validated LayerBo layerBo) {
        layerBo.setId(id);
        layerService.updateLayer(layerBo);
        return R.ok();
    }

    @Log(title = "图层管理-修改图层状态", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改图层状态 ", notes = "修改图层状态 ")
    @PutMapping("/changeStatus/{id}/{status}")
    public R<Void> changeArea(@PathVariable Long id, @ApiParam(value = "状态 1启用 2禁用", required = true) @PathVariable Integer status) {
        if (status != 1 && status != 2) {
            return R.fail("状态参数错误");
        }
        layerService.update(Wrappers.<Layer>lambdaUpdate().set(Layer::getStatus, status).eq(Layer::getId, id));
        return R.ok();
    }


    @Log(title = "图层管理-新增图层区域", businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增图层区域")
    @PostMapping("/saveLayer")
    public R<Void> saveLayer(@RequestBody @Validated LayerAreaBo layerAreaBo) {
        layerService.saveLayerArea(layerAreaBo);
        return R.ok();
    }


    @Log(title = "图层管理-修改图层区域", businessType = BusinessType.INSERT)
    @ApiOperation(value = "修改图层区域")
    @PostMapping("/updateLayer/{id}")
    public R<Void> updateLayer(@PathVariable Long id, @RequestBody @Validated LayerAreaBo layerAreaBo) {
        layerAreaBo.setId(id);
        layerService.updateLayerArea(layerAreaBo);
        return R.ok();
    }

    @ApiOperation("获取图层区域列表")
    @GetMapping("/listLayerArea/{layerId}")
    public TableDataInfoResult<List<LayerAreaDto>> listLayerArea(@PathVariable @ApiParam(value = "图层ID", required = true) Long layerId,
                                                                 @ApiParam(value = "区域名称", required = false) @RequestParam(required = false) String name,
                                                                 @RequestParam(required = false) @ApiParam(value = "开始时间 YYYY-MM-DD") String startTime,
                                                                 @RequestParam(required = false) @ApiParam(value = "结束时间 YYYY-MM-DD") String endTime) {
        Date start = null;
        Date end = null;
        if (StrUtil.isNotBlank(startTime)) {
            startTime = startTime + " 00:00:00";
            start = DateUtil.parseDateTime(startTime);
        }
        if (StrUtil.isNotBlank(endTime)) {
            endTime = endTime + " 23:59:59";
            end = DateUtil.parseDateTime(endTime);
        }
        startPage();
        List<LayerAreaDto> list = layerService.getLayerAreas(layerId, name, start, end);
        return getDataTableResult(list);
    }

    @ApiOperation("获取图层区域详情")
    @GetMapping("/getLayerAreaDetail/{areaId}")
    public R<LayerAreaDto> getLayerAreaDetail(@PathVariable @ApiParam(value = "图层区域ID", required = true) Long areaId) {
        LayerAreaDto layerAreaDto = layerService.getLayerArea(areaId);
        return R.ok(layerAreaDto);
    }


    @Log(title = "图层管理-删除图层", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除图层", notes = "删除图层")
    @DeleteMapping("/remove/{id}")
    public R<Void> remove(@PathVariable Long id) {
        layerService.removeById(id);
        return R.ok();
    }

    @Log(title = "图层管理-删除图层区域", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除图层区域", notes = "删除图层区域")
    @DeleteMapping("/removeLayerArea/{layerId}")
    public R<Void> removeLayerArea(@PathVariable Long layerId) {
        layerService.removeArea(layerId);
        return R.ok();
    }

    @Log(title = "图层管理-获取框选点位", businessType = BusinessType.OTHER)
    @ApiOperation(value = "获取框选点位", notes = "获取框选点位")
    @GetMapping("/polygonChannel")
    @Anonymous
    public R<List<Channel>> polygonChannel(String coordinateList){
        List<Channel> channelList = channelService.polygonChannel(StrUtil.split(coordinateList, ";"));
        return R.ok(channelList);
    }

}
